package com.sitanyun.merchant.guide.frament.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.AccountBean;
import com.sitanyun.merchant.guide.bean.SwiShopsBean;
import com.sitanyun.merchant.guide.bean.UpdateNameBean;
import com.sitanyun.merchant.guide.bean.UserInfo;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.frament.presenter.impl.UpdateNameAPresenterImpl;
import com.sitanyun.merchant.guide.frament.presenter.inter.IUpdateNameAPresenter;
import com.sitanyun.merchant.guide.frament.view.inter.IUpdateNameAView;
import com.sitanyun.merchant.guide.signature.GenerateTestUserSig;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.Aes;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.qcloud.tuicore.TUILogin;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 修改名字
 */
public class UpdateNameActivity extends BaseActivity implements IUpdateNameAView {

    @BindView(R.id.tv_return)
    ImageView tvReturn;
    @BindView(R.id.tv_title_str)
    TextView tvTitleStr;
    @BindView(R.id.image_right_key)
    ImageView imageRightKey;
    @BindView(R.id.tv_right_key)
    TextView tvRightKey;
    @BindView(R.id.ff_main_layout)
    FrameLayout ffMainLayout;
    @BindView(R.id.updat_ename)
    EditText updatEname;
    private IUpdateNameAPresenter mIUpdateNameAPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_update_name;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("更改姓名");
        tvRightKey.setVisibility(View.VISIBLE);
    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIUpdateNameAPresenter = new UpdateNameAPresenterImpl(this);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_right_key)
    public void onViewClicked() {
        if (updatEname.getText().toString().trim().isEmpty()) {
            ToastUtil.showToast(UpdateNameActivity.this, "请输入姓名");
            return;
        }
       mIUpdateNameAPresenter.setuser(updatEname.getText().toString().trim());
    }

    @Override
    public <T> T request(int requestFlag) {
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {
        if (responseFlag == IUpdateNameAView.RESPONSE_ONE) {
            UpdateNameBean    myUserInfoBean = (UpdateNameBean) response;
            if (myUserInfoBean.getCode() == 0) {
                ToastUtil.showToast(UpdateNameActivity.this, myUserInfoBean.getData()+"");
                SharedPreferenceUtil.SaveData("staffname",updatEname.getText().toString());
                if (SharedPreferenceUtil.getStringData("nodeid").equals(SharedPreferenceUtil.getStringData("tonodeid"))){
                    OkHttpUtils.delete().url(Urls.logout)
                            .addHeader("Content-Type","application/x-www-form-urlencoded")
                            .addHeader("Authorization","Bearer"+ SharedPreferenceUtil.getStringData("token"))
                            .build().execute(new StringCallbacks() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(String response, int id) {
                                AccountBean accountBean  = new Gson().fromJson(response, AccountBean.class);
                                if (accountBean.getCode()==0){
                                    //    mIUpdateNameAPresenter.setuser(updatEname.getText().toString().trim());

                                    Aes mAes = new Aes();
                                    try {
                                        String secret=  SharedPreferenceUtil.getStringData("cid")
                                                +","+SharedPreferenceUtil.getStringData("nodeid")+","
                                                +SharedPreferenceUtil.getStringData("staffid")+","
                                                +SharedPreferenceUtil.getStringData("userid");
                                        byte[]  secrets = secret.getBytes("UTF8");
                                        String   secretaec = mAes.encrypt(secrets);//加密

                                        HashMap<String, String> hashMap = new HashMap<String, String>();
                                        hashMap.put("staffId",SharedPreferenceUtil.getStringData("staffid"));
                                        hashMap.put("userId", SharedPreferenceUtil.getStringData("userid"));
                                        hashMap.put("clientId",  SharedPreferenceUtil.getStringData("cid"));//
                                        hashMap.put("nodeId", SharedPreferenceUtil.getStringData("nodeid") );//

                                        hashMap.put("secret",  secretaec);//

                                        OkHttpUtils.postString()
                                                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                                                .addHeader("Content-Type", "application/json")
                                                //   .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                                                .url(Urls.manual)
                                                .content(new Gson().toJson(hashMap))
                                                .build().execute(new StringCallbacks() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {

                                            }
                                            @Override
                                            public void onResponse(String response, int id) {
                                                SwiShopsBean loginBean= new Gson().fromJson(response, SwiShopsBean.class);
                                                if (loginBean.getCode() == 0) {
                                                    SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
                                                    SharedPreferenceUtil.SaveData("nodename", loginBean.getNode_name());
                                                    SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
                                                    SharedPreferenceUtil.SaveData("tonodename", loginBean.getTop_node_name()+"");
                                                    SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                                                    SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
                                                    SharedPreferenceUtil.SaveData("nodeid",loginBean.getNode_id());
                                                    SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
                                                    SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
                                                    SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());
                                                    SharedPreferenceUtil.SaveData("staffname", loginBean.getStaff_name());

                                                    // 获取userSig函数
                                                    String userSig = GenerateTestUserSig.genTestUserSig("staffid_"+loginBean.getStaff_id());
                                                    TUILogin.login("staffid_"+loginBean.getStaff_id(), userSig, new V2TIMCallback() {
                                                        @Override
                                                        public void onError(final int code, final String desc) {
                                                        }

                                                        @Override
                                                        public void onSuccess() {
                                                            //   ToastUtil.showToast(MainActivity.this,"腾讯云注册成功");
                                                            UserInfo.getInstance().setAutoLogin(true);
                                                            finish();
                                                        }
                                                    });

                                                } else {
                                                    ToastUtil.showToast(UpdateNameActivity.this,"错误");
                                                }
                                            }
                                        });
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }

                        }
                    });

                }else {

                    OkHttpUtils.delete().url(Urls.forcelogout)
                            .addHeader("Content-Type","application/x-www-form-urlencoded")
                            .addHeader("Authorization","Bearer"+ SharedPreferenceUtil.getStringData("token"))
                            .build().execute(new StringCallbacks() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(String response, int id) {
                            AccountBean accountBean  = new Gson().fromJson(response, AccountBean.class);

                                if (accountBean.getCode()==0){
                                    //    mIUpdateNameAPresenter.setuser(updatEname.getText().toString().trim());

                                    Aes mAes = new Aes();
                                    try {
                                        String secret=  SharedPreferenceUtil.getStringData("cid")
                                                +","+SharedPreferenceUtil.getStringData("nodeid")+","
                                                +SharedPreferenceUtil.getStringData("staffid")+","
                                                +SharedPreferenceUtil.getStringData("userid");
                                        byte[]  secrets = secret.getBytes("UTF8");
                                        String   secretaec = mAes.encrypt(secrets);//加密

                                        HashMap<String, String> hashMap = new HashMap<String, String>();
                                        hashMap.put("staffId",SharedPreferenceUtil.getStringData("staffid"));
                                        hashMap.put("userId", SharedPreferenceUtil.getStringData("userid"));
                                        hashMap.put("clientId",  SharedPreferenceUtil.getStringData("cid"));//
                                        hashMap.put("nodeId", SharedPreferenceUtil.getStringData("nodeid") );//

                                        hashMap.put("secret",  secretaec);//

                                        OkHttpUtils.postString()
                                                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                                                .addHeader("Content-Type", "application/json")
                                                //   .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                                                .url(Urls.manual)
                                                .content(new Gson().toJson(hashMap))
                                                .build().execute(new StringCallbacks() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {

                                            }
                                            @Override
                                            public void onResponse(String response, int id) {
                                                SwiShopsBean loginBean= new Gson().fromJson(response, SwiShopsBean.class);
                                                if (loginBean.getCode() == 0) {
                                                    SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
                                                    SharedPreferenceUtil.SaveData("nodename", loginBean.getNode_name());
                                                    SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
                                                    SharedPreferenceUtil.SaveData("tonodename", loginBean.getTop_node_name()+"");
                                                    SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                                                    SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
                                                    SharedPreferenceUtil.SaveData("nodeid",loginBean.getNode_id());
                                                    SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
                                                    SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
                                                    SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());
                                                    SharedPreferenceUtil.SaveData("staffname", loginBean.getStaff_name());

                                                    // 获取userSig函数
                                                    String userSig = GenerateTestUserSig.genTestUserSig("staffid_"+loginBean.getStaff_id());
                                                    TUILogin.login("staffid_"+loginBean.getStaff_id(), userSig, new V2TIMCallback() {
                                                        @Override
                                                        public void onError(final int code, final String desc) {
                                                        }

                                                        @Override
                                                        public void onSuccess() {
                                                            //   ToastUtil.showToast(MainActivity.this,"腾讯云注册成功");
                                                            UserInfo.getInstance().setAutoLogin(true);
                                                            finish();
                                                        }
                                                    });

                                                } else {
                                                    ToastUtil.showToast(UpdateNameActivity.this,"错误");
                                                }
                                            }
                                        });
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }

                        }
                    });

                }


//                ToastUtil.showToast(UpdateNameActivity.this, myUserInfoBean.getData()+"");
//                SharedPreferenceUtil.SaveData("staffname",updatEname.getText().toString());
//                OkHttpUtils.delete().url(Urls.forcelogout)
//                        .addHeader("Content-Type","application/x-www-form-urlencoded")
//                        .addHeader("Authorization","Bearer"+ SharedPreferenceUtil.getStringData("token"))
//                        .build().execute(new StringCallbacks() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        AccountBean accountBean  = new Gson().fromJson(response, AccountBean.class);
//                        if (accountBean.getCode()==0){
//                        //    mIUpdateNameAPresenter.setuser(updatEname.getText().toString().trim());
//
//                            Aes mAes = new Aes();
//                            try {
//                                String secret=  SharedPreferenceUtil.getStringData("cid")
//                                        +","+SharedPreferenceUtil.getStringData("nodeid")+","
//                                        +SharedPreferenceUtil.getStringData("staffid")+","
//                                        +SharedPreferenceUtil.getStringData("userid");
//                                byte[]  secrets = secret.getBytes("UTF8");
//                                String   secretaec = mAes.encrypt(secrets);//加密
//
//                                HashMap<String, String> hashMap = new HashMap<String, String>();
//                                hashMap.put("staffId",SharedPreferenceUtil.getStringData("staffid"));
//                                hashMap.put("userId", SharedPreferenceUtil.getStringData("userid"));
//                                hashMap.put("clientId",  SharedPreferenceUtil.getStringData("cid"));//
//                                hashMap.put("nodeId", SharedPreferenceUtil.getStringData("nodeid") );//
//
//                                hashMap.put("secret",  secretaec);//
//
//                                OkHttpUtils.postString()
//                                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
//                                        .addHeader("Content-Type", "application/json")
//                                        //   .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
//                                        .url(Urls.manual)
//                                        .content(new Gson().toJson(hashMap))
//                                        .build().execute(new StringCallbacks() {
//                                    @Override
//                                    public void onError(Call call, Exception e, int id) {
//
//                                    }
//                                    @Override
//                                    public void onResponse(String response, int id) {
//                                        SwiShopsBean loginBean= new Gson().fromJson(response, SwiShopsBean.class);
//                                        if (loginBean.getCode() == 0) {
//                                            SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
//                                            SharedPreferenceUtil.SaveData("nodename", loginBean.getNode_name());
//                                            SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
//                                            SharedPreferenceUtil.SaveData("tonodename", loginBean.getTop_node_name()+"");
//                                            SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
//                                            SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
//                                            SharedPreferenceUtil.SaveData("nodeid",loginBean.getNode_id());
//                                            SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
//                                            SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
//                                            SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());
//                                            SharedPreferenceUtil.SaveData("staffname", loginBean.getStaff_name());
//
//                                            // 获取userSig函数
//                                            String userSig = GenerateTestUserSig.genTestUserSig("staffid_"+loginBean.getStaff_id());
//                                            TUILogin.login("staffid_"+loginBean.getStaff_id(), userSig, new V2TIMCallback() {
//                                                @Override
//                                                public void onError(final int code, final String desc) {
//                                                }
//
//                                                @Override
//                                                public void onSuccess() {
//                                                    //   ToastUtil.showToast(MainActivity.this,"腾讯云注册成功");
//                                                    UserInfo.getInstance().setAutoLogin(true);
//                                                    finish();
//                                                }
//                                            });
//
//                                        } else {
//                                            ToastUtil.showToast(UpdateNameActivity.this,"错误");
//                                        }
//                                    }
//                                });
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//
//                        }
//                    }
//                });

            } else {
                ToastUtil.showToast(UpdateNameActivity.this, myUserInfoBean.getMsg()+"");
            }
        }
    }
}
